package yc.day0109.lottery.dao.impl;



import yc.day0109.lottery.dao.UserDao;
import yc.day0109.lottery.po.User;
import yc.day0109.lottery.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> queryUserByType(Map<String, Object> map) {
        List<User> userList = new ArrayList<>();
        User user = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" select id,user_name,mark_no,status,phone,to_char(open_date, 'yyyy-MM-dd')open_date from T_user ");
        sql.append(" where 1 = 1 ");
        if(map != null && map.size() > 0){
            Object markNo = map.get("markNo");
            if(markNo != null){
                sql.append(" and mark_no like ? ");
                rs = super.executeQuery(sql.toString(),new Object[]{ "%" + markNo + "%"});
            }
        }else {
            rs = super.executeQuery(sql.toString());
        }
        try{
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserName(rs.getString("user_name"));
                user.setMarkNo(rs.getString("mark_no"));
                user.setStatus(rs.getInt("status"));
                user.setPhone(rs.getString("phone"));
                user.setOpenDate(rs.getString("open_date"));
                userList.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return userList;
    }

    @Override
    public int addUser(User user) {
        int addRes = 0;
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into T_user ");
        sql.append(" (id,user_name,mark_no,status,phone,open_date)  ");
        sql.append(" values(T_user_id.nextval, ");
        sql.append(" ?,?,?,?,to_date(?,'yyyy-mm-dd')) ");
        addRes = super.executeUpdate(sql.toString(),new Object[]{user.getUserName(),user.getMarkNo(),
                user.getStatus(),user.getPhone(),user.getOpenDate()});
        return addRes;
    }
}
